package com.caterina.activities;
import java.awt.Container;
import java.util.ArrayList;
import java.util.Collection;

import modulesAPI.v2.CollectionButtonListener;
import modulesAPI.v2.Module;
import modulesAPI.v2.ModuleChangedListener;
import modulesAPI.v2.ViewType;
import modulesAPI.v2.basics.ModCollectionViewer;
import modulesAPI.v2.composite.MBudget;
import budget.Budget;
import budget.Service;

import com.caterina.activityManager.CActivity;
import com.caterina.components.CButton;
import com.caterina.components.COptionPane;
import com.caterina.components.COptionPane.MessageType;
import com.caterina.resources.CWords;
import controllers.PermissionList;
import controllers.ManagerTask.TaskType;


public class BudgetActivity extends CActivity implements ModuleChangedListener, CollectionButtonListener{
        public BudgetActivity(Container parent) {
                super("BUDGETS_ACTIVITY_TITLE",parent);
        }
       
        @Override
        public void onStart() {
            this.putTask(TaskType.BUDGET_GET_ALL, "");
        }
        @SuppressWarnings("unchecked")
        @Override
        public void onData(TaskType task, Object data) {
                switch(task){ 
                        case SERVICE_GET_ALL:{
                                this.services=(Collection<Service>) data;
                                this.reloadViewer();
                                break;
                        }
                        case BUDGET_GET_ALL:{
                                this.lastData=(Collection<Budget>) data;
                                this.putTask(TaskType.SERVICE_GET_ALL);
                                break;
                        }
                        case BUDGET_SEND:{
                                this.putTask(TaskType.BUDGET_GET_ALL,"");
                                break;
                        }
                        case BUDGET_MODIFY:{
                                this.putTask(TaskType.BUDGET_GET_ALL,"");
                                break;
                        }
                default:break;
                }
        }
        @Override
        public void onButtonPressed(Module<?> selectedItem, int buttonIndex) {
                this.reloadViewer();
        }
        @Override
        public void onError(TaskType task, Exception e) {
        	COptionPane.createTextOptionPane(MessageType.ERROR, CWords.getWord(e.getMessage()), this);
            this.putTask(TaskType.BUDGET_GET_ALL, "");
        }

        private void createButtons(){
                /*Tasto stipula*/
                this.stipulation=new CButton("BUDGET_SEND") {
                        @Override
                        public void onFocus() {}
                        @Override
                        public void onAction() {
                        	putTask(TaskType.BUDGET_SEND, collectionView.getSelectedContent().getContent());
                        }
                        private static final long serialVersionUID = 1L;
                };
                this.calculate=new CButton("BUDGET_CALCULATE") {
                        @Override
                        public void onFocus() {}
                        @Override
                        public void onAction() {
                                putTask(TaskType.BUDGET_MODIFY, collectionView.getSelectedContent().getContent());
                        }
                        private static final long serialVersionUID = 1L;
                };
        }
       
        private void reloadViewer(){
                int lastIndex=-1;
                if (this.collectionView!=null){
                        this.collectionView.removeModuleChangedListener(this);
                        lastIndex=this.collectionView.getSelectedIndex();
                }
                this.createButtons();
                ArrayList<Module<?>> budgetsMod=new ArrayList<Module<?>>();
                for (Budget b: this.lastData){
                        MBudget bdg=new MBudget(null, b,services,"BUDGET_FOR", "BUDGET_FOR_DESC", ViewType.WRITABLE);
                        bdg.addWritableButton(this.stipulation,this.calculate);
                        budgetsMod.add(bdg);
                }
                this.collectionView=new ModCollectionViewer(null, budgetsMod, "BUDGET","BUDGET_DESC", ViewType.WRITABLE,lastIndex,ModCollectionViewer.HIDE_ADD,ModCollectionViewer.HIDE_REMOVE);
                this.collectionView.addModuleChangedListener(this);
                this.add(collectionView);
        }
        @Override
        public void onModuleChange(Module<?> mod) {
                if (mod.getTag().equals(CWords.getWord("BUDGET_SERVICES"))){
                        ArrayList<Budget> list=new ArrayList<Budget>();
                        for(Module<?> modSel:this.collectionView.getContent()){
                                list.add((Budget) modSel.getContent());
                        }
                        this.lastData=list;
                        this.reloadViewer();
                }else this.add(collectionView);
        }
        @SuppressWarnings("unused")
        private PermissionList permissions;
        private Collection<Budget>lastData;
        private CButton stipulation;
        private CButton calculate;
        private Collection<Service>services;
        private ModCollectionViewer collectionView;
        private static final long serialVersionUID = 1L;
}
